package com.example.wangandroid.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.example.common.TreeClass
import com.example.wangandroid.R


class TreeAdapter(private var data: TreeClass): RecyclerView.Adapter<TreeAdapter.ViewHolder>() {


    class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        var title:TextView = itemView.findViewById(R.id.tv_name)
        var text:TextView = itemView.findViewById(R.id.tv_listname)
        var ll:LinearLayout = itemView.findViewById(R.id.ll)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
       val view = LayoutInflater.from(parent.context).inflate(R.layout.tree_item,parent,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {

        return data.data.size

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val userPoint = data.data[position]
        LogUtils.d(userPoint)
        holder.title.text = userPoint.name
        var string:String = ""
        for (str in userPoint.children){
            string += str.name + "      "
        }
        holder.text.text = string
        holder.ll.setOnClickListener {
            LogUtils.d("id==========="+userPoint.id)
            ARouter.getInstance().build("/app/listArticle")
                .withInt("id",userPoint.id)
                .withInt("cid",0)
                .navigation();
        }
    }

    fun setData(it: TreeClass?) {
        if (it != null) {
            data = it
        }
    }

}